package dongshi.daddy.zhengxi;

public class DeadLock {

    public static Object lock1 = new Object();
    public static Object lock2 = new Object();

    public static void main(String[] args) {
        Thread a = new Thread(new Lock1(), "DeadLock1");
        Thread b = new Thread(new Lock2(), "DeadLock2");
        a.start();
        b.start();
    }
}

class Lock1 implements Runnable {
    @Override
    public void run() {
        try {
            while (true) {
                synchronized (DeadLock.lock1) {
                    System.out.println("Waiting for lock2");
                    Thread.sleep(3000);
                    synchronized (DeadLock.lock2) {
                        System.out.println("Lock1 acquired lock1 and lock2 ");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Lock2 implements Runnable {
    @Override
    public void run() {
        try {
            while (true) {
                synchronized (DeadLock.lock2) {
                    System.out.println("Waiting for lock1");
                    Thread.sleep(3000);
                    synchronized (DeadLock.lock1) {
                        System.out.println("Lock2 acquired lock1 and lock2");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
